﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Zero.DataLayer;
using Zero.DataLayer.Repository;
using Zero.POCO;
using Zero.POCO.Compagne;
using Zero.POCO.Donnees;
using Zero.Presenter;

namespace DevExpress.MailClient.Win.Forms.Campagne
{
    public partial class frmEditCompagne : XtraForm, IEditCampagneView
    {
        public List<Vague> Vague { get; set; }
        public List<Annonceur> Annonceurs { get; set; }
        public List<Filter> Filters { get; set; }
        public List<PlanView> PlanTvs { get; set; }
        public List<PlanView> PlanRds { get; set; }
        public List<PlanView> PlanPresses { get; set; }
      

        private EditionCampagnePresenter editionCampagnePresenter;

        bool _isCampaignModified;
        public Compaign sourceCompaign { get; set; }

        public frmEditCompagne()
        {
            InitializeComponent();

            DialogResult = DialogResult.Cancel;
        }


        public frmEditCompagne(Compaign compaign, bool newCampaign, string caption)
        {
            InitializeComponent();
            if (newCampaign)
            {
                //    BBIView.Enabled = false;
                //   BBISupp.Enabled = false;
                //   BBINext.Enabled = false;
                // BBIPrev.Enabled = false;
                sourceCompaign = new Compaign();
                sourceCompaign.id = default(Guid);

            }
            else
            {
                if (compaign != null)
                {
                    sourceCompaign = compaign;
                    sourceCompaign.id = compaign.id;
                }
            }



            editionCampagnePresenter = new EditionCampagnePresenter(this, new VagueRepository(), new AnnonceurRepository(), new FilterRepository(), new PlanTvRepository(), new PlanRdRepository(), new PlanPrRepository());
            editionCampagnePresenter.Display();
            SLUAnnonceur.Properties.DataSource = Annonceurs;
            var b = SLUAnnonceur.EditValue;
            if (!Annonceurs.Any() || Annonceurs == null || SLUAnnonceur.EditValue == null) SLUMarque.Properties.DataSource = new List<Marque>();
            else
            {
                var listeMarques = Annonceurs.First(a => a.id.ToString() == SLUAnnonceur.EditValue).Marques;
                SLUMarque.Properties.DataSource = listeMarques.Any() ? listeMarques : new List<Marque>();
            }
            SLUCible.Properties.DataSource = Filters;

            SLUVague.Properties.DataSource = Vague;

            

            /* GCTele.DataSource = PlanTvs;
             GVTv.Columns[0].Visible = false;
             GCRadio.DataSource = PlanRds;
             GVRadio.Columns[0].Visible = false;
             GCPresse.DataSource = PlanPresses;
             GVPresse.Columns[0].Visible = false;
             */




            // DictionaryHelper.InitDictionary(spellChecker1);
            DialogResult = DialogResult.Cancel;
            
            _isCampaignModified = newCampaign;




        }

        public bool IsCampaignModified
        {
            get { return _isCampaignModified; /*|| richEditControl.Modified;*/ }
            set
            {
                _isCampaignModified = value;
                //richEditControl.Modified = value;
            }
        }


        private void SLUAnnonceur_EditValueChanged(object sender, System.EventArgs e)
        {
            _isCampaignModified = true;
            var key = ((Annonceur)SLUAnnonceur.EditValue).id;if (key == null) return;
            if (!Annonceurs.Any() || Annonceurs == null || SLUAnnonceur.EditValue == null) SLUMarque.Properties.DataSource = new List<Marque>();
            else
            {
                var listeMarques = Annonceurs.First(a => a.id.ToString() == key.ToString()).Marques;
                SLUMarque.Properties.DataSource = listeMarques.Any() ? listeMarques : new List<Marque>();
            }
        }

        private void TEName_EditValueChanged(object sender, System.EventArgs e)
        {
            _isCampaignModified = true;
        }

        private void SLUMarque_EditValueChanged(object sender, System.EventArgs e)
        {
            _isCampaignModified = true;
        }

        private void CBETypeCompagne_EditValueChanged(object sender, System.EventArgs e)
        {
            _isCampaignModified = true;
        }

        private void DEDebut_EditValueChanged(object sender, System.EventArgs e)
        {
            _isCampaignModified = true;
        }

        private void DEFin_EditValueChanged(object sender, System.EventArgs e)
        {
            _isCampaignModified = true;
        }

        private void SLUCible_EditValueChanged(object sender, System.EventArgs e)
        {
            _isCampaignModified = true;
        }

        private void SLUVague_EditValueChanged(object sender, System.EventArgs e)
        {
            _isCampaignModified = true;
        }

        private void frmEditCompagne_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult result = QueryClose();
            e.Cancel = result == DialogResult.Cancel;
        }


           DialogResult QueryClose()
        {
            if (!IsCampaignModified)
                return DialogResult.Yes;

            DialogResult result = XtraMessageBox.Show(this, Properties.Resources.SaveQuestion, Application.ProductName, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation);
            switch (result)
            {
                case DialogResult.Cancel:
                    return DialogResult.Cancel;
                case DialogResult.No:
                    return DialogResult.No;
                default:
                case DialogResult.Yes:
                    ApplyChanges();
                    return DialogResult.Yes;
            }
        }


        void ApplyChanges()
        {

            /*if (sourceCompaign.id == default(Guid))
                sourceCompaign.id = Guid.NewGuid();*/

            sourceCompaign.Name = TEName.Text;
            sourceCompaign.DateDebut = DEDebut.DateTime;
            sourceCompaign.DateFin = DEFin.DateTime;
            sourceCompaign.Annonceur = (Annonceur)SLUAnnonceur.EditValue;
            sourceCompaign.Marque = (Marque)SLUMarque.EditValue;
            sourceCompaign.Vague = (Vague)SLUVague.EditValue;
            sourceCompaign.Filtre = (Filter)SLUCible.EditValue;
            IsCampaignModified = false;

        }

        private void SLUAnnonceur_Popup(object sender, EventArgs e)
        {
            SLUAnnonceur.Properties.View.Columns[1].Visible = false;
            SLUAnnonceur.Properties.View.Columns[2].Visible = false;
        }

        private void SLUMarque_Popup(object sender, EventArgs e)
        {
            SLUMarque.Properties.View.Columns[2].Visible = false;
        }

        private void SLUCible_Popup(object sender, EventArgs e)
        {
            SLUCible.Properties.View.Columns[3].Visible = false;
            SLUCible.Properties.View.Columns[4].Visible = false;
            SLUCible.Properties.View.Columns[5].Visible = false;
            SLUCible.Properties.View.Columns[6].Visible = false;
            SLUCible.Properties.View.Columns[7].Visible = false;
            SLUCible.Properties.View.Columns[8].Visible = false;
            SLUCible.Properties.View.Columns[9].Visible = false;
            SLUCible.Properties.View.Columns[10].Visible = false;
            SLUCible.Properties.View.Columns[11].Visible = false;
            SLUCible.Properties.View.Columns[12].Visible = false;
            SLUCible.Properties.View.Columns[13].Visible = false;
            SLUCible.Properties.View.Columns[14].Visible = false;
            SLUCible.Properties.View.Columns[15].Visible = false;
            //SLUCible.Properties.View.Columns[16].Visible = false;

            SLUCible.Properties.View.Columns[17].Visible = false;}
        private void SLUVague_Popup(object sender, EventArgs e)
        {
            SLUVague.Properties.View.Columns[0].Visible = false;
            SLUVague.Properties.View.Columns[3].Visible = false;
            SLUVague.Properties.View.Columns[5].Visible = false;
            SLUVague.Properties.View.Columns[6].Visible = false;
            /* SLUVague.Properties.View.Columns[8].Visible = false;
            SLUVague.Properties.View.Columns[9].Visible = false;
            SLUVague.Properties.View.Columns[10].Visible = false;*/
        }

        private void SLUAnnonceur_AddNewValue(object sender, XtraEditors.Controls.AddNewValueEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            var form = new frmEditAnnonceur(null, true);
            
            form.Location = new Point(Left + (Width - form.Width) / 2, Top + (Height - form.Height) / 2);
            form.ShowDialog();
            editionCampagnePresenter.Display();
            SLUAnnonceur.Properties.DataSource = Annonceurs;
            Cursor.Current = Cursors.Default;
        }

        private void SLUMarque_AddNewValue(object sender, XtraEditors.Controls.AddNewValueEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            var form = new frmEditMarque(null, true, Annonceurs);
            // form.Load += OnEditMailFormLoad;
            // form.FormClosed += OnEditMailFormClosed;
            form.Location = new Point(Left + (Width - form.Width) / 2, Top + (Height - form.Height) / 2);
            form.ShowDialog();
            form.Activate();
            Cursor.Current = Cursors.Default;
        }

        private void ButtonAnnuler_Click(object sender, EventArgs e)
        {

        }

        private void ButtonSauvegarder_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Yes;
            _isCampaignModified = false;
            DialogResult result = QueryClose();
            ApplyChanges();
            Close();
        }

        private void BBIAddTv_ItemClick(object sender, XtraBars.ItemClickEventArgs e)
        {

        }

        private void BBIenleverTv_ItemClick(object sender, XtraBars.ItemClickEventArgs e)
        {

        }

        private void BBIModifierTv_ItemClick(object sender, XtraBars.ItemClickEventArgs e)
        {

        }

        private void BBIAjouterPr_ItemClick(object sender, XtraBars.ItemClickEventArgs e)
        {

        }

        private void BBIAjouterRd_ItemClick(object sender, XtraBars.ItemClickEventArgs e)
        {

        }

        private void BBIENleverPr_ItemClick(object sender, XtraBars.ItemClickEventArgs e)
        {

        }

        private void BBIModifierPr_ItemClick(object sender, XtraBars.ItemClickEventArgs e)
        {

        }

        private void BBIModifierRd_ItemClick(object sender, XtraBars.ItemClickEventArgs e)
        {

        }


    }
}